<?php
defined('BY_JYA') or exit('error');
$return=array();
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
$return['account']=pdo_get('core_accounts',array('uniacid'=>$_SESSION['uniacid']));
$return['account']['navs']=json_decode($return['account']['navs'],true);
$return['account']['links']=json_decode($return['account']['links'],true);
$plugin_name=pdo_getcolumn('core_account_group',array('id'=>$return['account']['groupid']),'plugin_name');
$return['account']['pluginurl']=$plugin_name?custom_url($plugin_name.'/index/',true):'jy_weishop';
$return['extradata']=array();
if($_RQ['op']=='list'){
	$fenlei=pdo_getall('core_account_article_fenlei',array('uniacid'=>$_SESSION['uniacid']),array('id','title','picurl'),'','paixu desc, id desc');
	$return['url']=custom_url('core/welcome.home/',true);
	
	if($fenlei){
		foreach($fenlei as &$item){
			$item['url']=custom_url('core/article.list//group_id='.$item['id'],true);
			$item['picurl'] = $item['picurl'] ? $item['picurl'] : '../core/resource/images/nopic.jpg';
		}
	}
	$return['item']['fenleiurl']=custom_url('core/article.list//fenlei='.$return['item']['fenlei'],true);
	$params=array('uniacid'=>$_SESSION['uniacid'],'status'=>1);
	if($_RQ['title']){
		$params['title like']='%'.trim($_RQ['title']).'%';
		$return['params']['title']=$_RQ['title'];
	}
	$group_id=intval($_RQ['group_id']);
	if($group_id){
		$params['fenlei']=$group_id;
		$return['params']['group_id']=$group_id;
	}
	$ishot=intval($_RQ['ishot']);
	if($ishot){
		$params['ishot']=1;
		$return['params']['ishot']=1;
	}
	$order=intval($_RQ['order']);
	if($order==1){
		$order='id desc';
	}elseif($order==2){
		$order='ishot desc, id desc';
	}else{
		$order='readnum desc, id desc';
	}
	$fields=array('id','title','content','fenlei','keyword','createtime','readnum','re_readnum','likenum','status','desc','pic_url','yingxiao');
	$orderby=array();
	$_RQ['page'] = $_RQ['page'] ? $_RQ['page'] : 1;
	$pindex = max(1, intval($_RQ['page']));
	if(intval($_RQ['num'])){
		$psize = intval($_RQ['num']);
	}else{
		$psize = 10;
	}
	$total='';
	$list=pdo_getslice('core_account_article',$params,array($pindex,$psize),$total,$fields,'',$order);
	$nums=36;
	if($_SESSION['os']=='windows'){
		$nums=180;
	}
	if($list){
		foreach($list as &$item){
			$item['desc2'] = strip_tags(str_replace('&nbsp;','',$item['content']));
			$item['desc'] = $item['desc'] ? $item['desc'] : cfc('tools')->substr($item['desc2'],0,$nums);
			$item['picurl'] = $item['pic_url'] ? $item['pic_url'] : '../core/resource/images/nopic.jpg';
			$item['createtime']=date('Y/m/d H:i:s',$item['createtime']);
			$item['fenlei']=$fenlei[$item['fenlei']]['title'];
			$item['edit']=array('op'=>'edit','action'=>'article.list','query'=>'id='.$item['id']);
			$item['url']=custom_url('core/article.list/detail/id='.$item['id'],true);
		}
	}
	$sharelink='core/article.list/list/fromuser='.$_SESSION['uid'].'&group_id='.$group_id;
	$return['sharedata']=array(
			'title'=>$_RQ['group_id'] ? $fenlei[$group_id]['title'] . '-' . $_SESSION['account']['title']: '文章分类 -'.$_SESSION['account']['title'],
			'desc'=>$_SESSION['account']['title'].'-文章列表',
			'link'=>custom_url($sharelink,true),
			'imgUrl'=>tomedia($return['account']['logo']),
	);
	$hotlist=$list;
	paixu($hotlist,'re_readnum','desc');
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['group_id']=$group_id?: 0;
	$return['fenleiname']=$fenlei[$group_id]['title'];
	$return['groups']=$fenlei?$fenlei:array();
	$return['list']=$list?$list:array();
	$return['hotlist']=$hotlist?$hotlist:array();
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	set_html('title',$return['fenleiname'].'-'.$_SESSION['account']['title']);
	exi($return);
}
if($_RQ['op']=='detail'){
	//刷新阅读次数
	pdo_update('core_account_article',array('re_readnum +='=>1),array('id'=>$_RQ['id'],'uniacid'=>$_SESSION['uniacid']));
	//获取文章数据
	$article_id=intval($_RQ['id']);
	$return['onlycontent']=intval($_RQ['onlycontent']);
	$return['item']=pdo_get('core_account_article',array('id'=>$article_id,'uniacid'=>$_SESSION['uniacid']));
	if(!$return['item']){
		exi('指定文章不存在！','error','list');
	}
	if($_RQ['fromuser'] && $_RQ['fromuser']!=$_SESSION['uid']){
		model('core/article')->up_yingxiao($return['item'],$_RQ['fromuser']);
	}
	$article_rule_userd_money=pdo_getsum('core_credit_log',array('uniacid'=>$_SESSION['uniacid'],'type'=>2,'plugin'=>'core','tag'=>'article_'.$return['item']['id']),'fee');
	$return['item']['article_rule_userd_money']=$article_rule_userd_money>$return['item']['article_rule_userd_money']?$article_rule_userd_money:$article_rule_userd_money;
	//是否付费
	if($return['item']['fee']>0){
		$order=pdo_get('core_account_article_order',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid'],'article_id'=>$return['item']['id']));
		if($order['ispay']==1){
			$return['item']['noread']=false;
		}else{
			$return['item']['noread']=true;
		}
		if($order['id']){
			$return['order_id']=$order['id'];
		}
	}
	$return['group_id']=$return['item']['fenlei'];
	$return['item']['readnum'] = $return['item']['readnum']+$return['item']['re_readnum'];
	unset($return['item']['re_readnum']);
	//点赞数=虚拟点赞数+真实点赞数
	$return['item']['likenum']+=pdo_count('core_account_article_like',array('article_id'=>$article_id,'like'=>1));
	//unset($return['item']['re_likenum']);
	$return['item']['advs']=json_decode($return['item']['advs'],true);
	$return['item']['createtime']=date('20y-m-d',$return['item']['createtime']);
	$return['fenlei']=pdo_getall('core_account_article_fenlei',array('uniacid'=>$_SESSION['uniacid']),array('id','title'));
	$return['item']['fenleiurl']=custom_url('core/article.list//id='.$return['item']['fenlei'],true);
	$return['item']['fenlei']=$return['fenlei'][$return['item']['fenlei']]['title'];
	$advs=array();
	if($return['item']['yingxiao']==1){
		$advs[]=$return['item']['advs'][0];
	}elseif($return['item']['yingxiao']==2){
		$num=count($return['item']['advs']);
		$return['index']=mt_rand(0,$num-1);
		$advs[]=$return['item']['advs'][$return['index']];
	}else{
		$advs=$return['item']['advs'];
	}
	$return['item']['advs']=$advs;
	//去掉多余空样式
	$return['item']['content']=str_replace('style=""','',$return['item']['content']);
	//$return['item']['content'] = str_replace('&nbsp;','',$return['item']['content']);
	$content=strip_tags($return['item']['content']);
	$return['item']['desc'] = $return['item']['desc'] ? $return['item']['desc'] : cfc('tools')->substr($content,0,36);
	$sharelink='core/article.list/detail/fromuser='.$_SESSION['uid'].'&id='.$return['item']['id'];
	$return['sharedata']=array(
			'title'=>$return['item']['title'] . '-' . $_SESSION['account']['title'],
			'desc'=>$return['item']['desc'],
			'link'=>custom_url($sharelink,true),
			'imgUrl'=>$return['item']['pic_url']?tomedia($return['item']['pic_url']):tomedia($return['account']['logo']),
	);
	//检查当前用户是否点赞
	if($_SESSION['uid']){
		$return['like']=pdo_get('core_account_article_like',array('article_id'=>$article_id,'mid'=>$_SESSION['uid']),array('id','like'));
		
	}
	//获取站点内容页模板
	if($_RQ['siteid'] && $_SESSION['os']=='windows'){
		$siteid=intval($_RQ['siteid']);
		$return['tpl']= cfc('template')->get_html($siteid,'desc');
		$return['htmls']=<<<TPL
<div ng-include="tomedia(pagedata.tpl.head_html)"></div>
<div ng-include="tomedia(pagedata.tpl.type)"></div>
<div ng-include="tomedia(pagedata.tpl.foot_html)"></div>
TPL;
	}
	$params=array('uniacid'=>$_SESSION['uniacid']);
	if($_RQ['title']){
		$params['title like']='%'.trim($_RQ['title']).'%';
		$return['params']['title']=$_RQ['title'];
	}
	$fenleiid=intval($_RQ['group_id']);
	if($fenleiid){
		$params['fenlei']=$fenleiid;
	}
	$fields=array('id','title','fenlei','keyword','createtime','readnum','re_readnum','likenum','status','desc','pic_url','yingxiao');
	$orderby=array();
	$_RQ['page'] = $_RQ['page'] ? $_RQ['page'] : 1;
	$pindex = max(1, intval($_RQ['page']));
	if(intval($_RQ['num'])){
		$psize = intval($_RQ['num']);
	}else{
		$psize = 10;
	}
	$total='';
	$list=pdo_getslice('core_account_article',$params,array($pindex,$psize),$total,$fields,'','id desc');
	if($list){
		foreach($list as &$item){
			$item['desc2'] = strip_tags(str_replace('&nbsp;','',$item['desc2']));
			$item['desc'] = $item['desc'] ? $item['desc'] : cfc('tools')->substr($item['desc2'],0,36);
			$item['picurl'] = $item['pic_url'] ? $item['pic_url'] : '../core/resource/images/nopic.jpg';
			$item['createtime']=date('Y/m/d H:i:s',$item['createtime']);
			$item['fenlei']=$fenlei[$item['fenlei']]['title'];
			$item['edit']=array('op'=>'edit','action'=>'article.list','query'=>'id='.$item['id']);
			$item['url']=custom_url('core/article.list/detail/id='.$item['id'],true);
		}
	}
	$hotlist=$list;
	paixu($hotlist,'re_readnum','desc');
	$return['list']=$list?$list:array();
	$return['hotlist']=$hotlist?$hotlist:array();
	set_html('title',$return['item']['title']);
	exi($return);
}
if($_RQ['op']=='like'){
	$post=get_postdata();
	if(!$_SESSION['uid']){
		cfc('core')->checklogin();
	}
	$id=intval($post['id']);
	$like=intval($post['like']);
	$data=array(
		'like'=>$post['like']?1:0,
		'updatetime'=>TIMESTAMP,
	);
	if($id){
		pdo_update('core_account_article_like',$data,array('id'=>$id,'mid'=>$_SESSION['uid']));
	}else{
		$data['uniacid']=$_SESSION['uniacid'];
		$data['article_id']=intval($post['article_id']);
		$data['mid']=$_SESSION['uid'];
		pdo_insert('core_account_article_like',$data);
	}
	exi('点赞成功！');
}
if($_RQ['op']=='pay'){
	$postdata=get_postdata();
	$article_id = $postdata['article_id'];
	//判断待支付订单及判断是否超过可预订次数
	$article=pdo_get('core_account_article',array('id'=>$article_id));
	$insert=array(
			'uniacid'=>$_SESSION['uniacid'],
			'mid'=>$_SESSION['uid'],
			'article_id'=>$article_id,
			'fee'=>$article['fee'],
			'createtime'=>TIMESTAMP,
	);
	pdo_insert('core_account_article_order',$insert);
	$id=pdo_insertid();
	$params=array(
					'id'=>$id,
					'title'=>'文章付费阅读',
					'fee'=>$article['fee'],
					'plugin'=>'core',
					//'pay_type'=>'wechat',//可选，此项作用为指定支付方式
					'no_type'=>array('hdfk'),//可选，此项作用为指定不允许使用哪种支付方式
					'back_url'=>'core/article.list/payresult/',//必填，指定异步支付结果通知地址，采用系统标准URL书写格式
					'success_url'=>'core/article.list/detail/id='.$article_id,
					'error_url'=>'core/article.list/detail/id='.$article_id,
	);
	$payid=cfc('pay')->create_pay_order($params);
	pdo_update('core_account_article_order',array('pay_id'=>$payid),array('id'=>$id));
	$res=array(
		'pay_id'=>$payid,
		'order_id'=>$id,
	);
	exi($res);
}
if($_RQ['op']=='twopay'){
	$post=get_postdata();
	$id=$post['id'];
	if(!$id){
		exi('订单不存在，请重新选择订单','error');
	}
	$pay_id=pdo_getcolumn('core_account_article_order',array('uniacid'=>$_SESSION['uniacid'],'id'=>$id),'pay_id');
	exi($pay_id);
}
if($_RQ['op']=='payresult'){
	$payresult=$_SESSION['pay_result'];
	if($payresult['status']!=1 && $payresult['pay_type'] !='hdfk'){
		exi('支付失败。','error','core/article.list/detail/id='.$payresult['id']);
	}
	$order=pdo_get('core_account_article_order',array('id'=>$payresult['id']));
	if($order['ispay'] ==1){
		exi('该订单已经支付！','error','core/article.list/detail/id='.$payresult['id']);
	}
	if($order['fee']!=$payresult['fee']){
		exi('支付失败！订单金额不匹配！'.$order['fee'].'|'.$payresult['fee'],'error','core/article.list/detail/id='.$payresult['id']);
	}
	pdo_update('core_account_article_order',array('ispay'=>1),array('id'=>$payresult['id']));
	exi('提交成功!');
}